package org.dromara.bean.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dromara.bean.domain.HisHospital;
import org.dromara.bean.domain.bo.HisHospitalBo;
import org.dromara.bean.domain.vo.HisHospitalVo;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import org.dromara.bean.domain.bo.HisDepBo;
import org.dromara.bean.domain.vo.HisDepVo;
import org.dromara.bean.domain.HisDep;
import org.dromara.bean.mapper.HisDepMapper;
import org.dromara.bean.service.IHisDepService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 医院科室Service业务层处理
 *
 * @author Lion Li
 * @date 2024-10-17
 */
@RequiredArgsConstructor
@Service
public class HisDepServiceImpl implements IHisDepService {

    private final HisDepMapper baseMapper;


    /**
     * 查询医院科室
     *
     * @param id 主键
     * @return 医院科室
     */
    @Override
    public HisDepVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }




    /**
     * 分页查询医院列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 医院分页列表
     */
    @Override
    public TableDataInfo<HisDepVo> queryPageList(HisDepBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisDep> lqw = buildQueryWrapper(bo);
        Page<HisDepVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的医院科室列表
     *
     * @param bo 查询条件
     * @return 医院科室列表
     */
    @Override
    public List<HisDepVo> queryList(HisDepBo bo) {
        LambdaQueryWrapper<HisDep> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisDep> buildQueryWrapper(HisDepBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisDep> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), HisDep::getTitle, bo.getTitle());
        lqw.eq(bo.getParentId() != null, HisDep::getParentId, bo.getParentId());
        return lqw;
    }

    /**
     * 新增医院科室
     *
     * @param bo 医院科室
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HisDepBo bo) {
        HisDep add = MapstructUtils.convert(bo, HisDep.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改医院科室
     *
     * @param bo 医院科室
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HisDepBo bo) {
        HisDep update = MapstructUtils.convert(bo, HisDep.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisDep entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除医院科室信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 只查询一级
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<HisDepVo> list(HisDepBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisDep> lqw = new LambdaQueryWrapper<>();
        lqw.eq(HisDep::getParentId, 0)
            .eq(HisDep::getDelFlag, 0);
        Page<HisDepVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
}
